using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Common.Shared.Windows.Forms {
  public abstract class FileSystemTreeNode : TreeNode {
    protected FileInfo fileInfo = null;
    protected DirectoryInfo directoryInfo = null;

    public event TreeViewCancelEventHandler BeforeCollapse;
    public event TreeViewCancelEventHandler BeforeCheck;
    public event TreeViewCancelEventHandler BeforeExpand;
    public event NodeLabelEditEventHandler BeforeLabelEdit;
    public event TreeViewCancelEventHandler BeforeSelect;
    public event TreeViewEventHandler AfterCheck;
    public event TreeViewEventHandler AfterCollapse;
    public event TreeViewEventHandler AfterExpand;
    public event NodeLabelEditEventHandler AfterLabelEdit;
    public event TreeViewEventHandler AfterSelect;


    public FileSystemTreeNode ( ) {

    }

    public DirectoryInfo DirectoryInfo { get { return this.directoryInfo == null ? fileInfo == null ? null : fileInfo.Directory : this.directoryInfo; } }
    public FileInfo FileInfo { get { return this.fileInfo; } }

    public void Refresh ( ) {
      this.Nodes.Clear ( );
      if ( this.DirectoryInfo != null && this.FileInfo == null ) {
        if ( this.DirectoryInfo.GetDirectories ( ).Length > 0 || this.DirectoryInfo.GetFiles ( ).Length > 0 )
          this.Nodes.Add ( new DummyTreeNode ( ) );
      }
    }

    public virtual void SetIcons ( SystemImageList imageList, bool forceLoadFromDisk, ShellIconStateConstants state ) {
      if ( imageList != null ) {
        if ( this.fileInfo != null ) {
          this.ImageIndex = this.SelectedImageIndex = imageList.IconIndex ( this.fileInfo.FullName, forceLoadFromDisk, state );
        } else if ( this.directoryInfo != null ) {
          this.ImageIndex = this.SelectedImageIndex = imageList.IconIndex ( this.directoryInfo.FullName, forceLoadFromDisk, state );
        }
      }
    }

    public virtual void SetIcons ( SystemImageList imageList ) {
      this.SetIcons ( imageList, ShellIconStateConstants.ShellIconStateNormal );
    }


    public virtual void SetIcons ( SystemImageList imageList, ShellIconStateConstants state ) {
      bool force = this.fileInfo == null;
      this.SetIcons ( imageList, force, state );
    
    }
    /// <summary>
    /// Raises the <see cref="E:BeforeCheck"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnBeforeCheck ( TreeViewCancelEventArgs e ) {
      if ( this.BeforeCheck != null )
        this.BeforeCheck ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:BeforeCollapse"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnBeforeCollapse ( TreeViewCancelEventArgs e ) {
      if ( this.BeforeCollapse != null )
        this.BeforeCollapse ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:BeforeExpand"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnBeforeExpand ( TreeViewCancelEventArgs e ) {
      if ( this.BeforeExpand != null )
        this.BeforeExpand ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:BeforeLabelEdit"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.NodeLabelEditEventArgs"/> instance containing the event data.</param>
    internal virtual void OnBeforeLabelEdit ( NodeLabelEditEventArgs e ) {
      if ( this.BeforeLabelEdit != null )
        this.BeforeLabelEdit ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:BeforeSelect"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnBeforeSelect ( TreeViewCancelEventArgs e ) {
      if ( this.BeforeSelect != null )
        this.BeforeSelect ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:AfterCheck"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnAfterCheck ( TreeViewEventArgs e ) {
      if ( this.AfterCheck != null )
        this.AfterCheck ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:AfterCollapse"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnAfterCollapse ( TreeViewEventArgs e ) {
      if ( this.AfterCollapse != null )
        this.AfterCollapse ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:AfterExpand"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnAfterExpand ( TreeViewEventArgs e ) {
      if ( this.AfterExpand != null )
        this.AfterExpand ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:AfterLabelEdit"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.NodeLabelEditEventArgs"/> instance containing the event data.</param>
    internal virtual void OnAfterLabelEdit ( NodeLabelEditEventArgs e ) {
      if ( this.AfterLabelEdit != null )
        this.AfterLabelEdit ( this, e );
    }

    /// <summary>
    /// Raises the <see cref="E:AfterSelect"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    internal virtual void OnAfterSelect ( TreeViewEventArgs e ) {
      if ( this.AfterSelect != null )
        this.AfterSelect ( this, e );
    }
  }


}
